package com.centriole.service.workflow.controller;

import com.centriole.common.utils.JsonUtil;
import com.centriole.service.workflow.entity.OrgnizationNode;
import com.centriole.service.workflow.entity.ShiroRole;
import com.centriole.service.workflow.entity.shiro.Role;
import com.centriole.service.workflow.entity.shiro.ShiroUser;
import com.centriole.service.workflow.service.shiro.IRoleService;
import com.centriole.service.workflow.service.shiro.IShiroUserService;
import com.centriole.service.workflow.util.OkHttpUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping(value = "/bap",produces = "application/json;charset=utf-8")
public class BapController {
    @Resource
    private IShiroUserService userService;
    @Resource
    private IRoleService roleService;

    /**
     * 提供json格式数据
     *
     * @param sortName  排序属性
     * @param sortOrder 排序类型 {asc, desc}
     * @param currPage  当前页
     * @param pageSize  页大小
     * @param session   session
     */
    @ResponseBody
    @RequestMapping("/list/json")
    public Object listJson(@RequestParam(value = "sortname", required = false, defaultValue = "") String sortName,
                           @RequestParam(value = "sortorder", required = false, defaultValue = "") String sortOrder,
                           @RequestParam(value = "page", required = false, defaultValue = "") String currPage,
                           @RequestParam(value = "pagesize", required = false, defaultValue = "") String pageSize,
                           HttpSession session) {
        Map<String, Object> obj = new HashMap<>();
        List<Role> list;
        try {
            //调用指定接口
            String dataInterfaces = (String) session.getAttribute("dataInterfaces");
            if(StringUtils.isNotBlank(dataInterfaces)){
                 String roleUrl = dataInterfaces.split(",")[1];  //[0]是用户数据接口 ， [1]是角色数据接口
                String roleJson = OkHttpUtil.get(roleUrl, null);
                 list = JsonUtil.jsonToList(roleJson, Role.class);
            }else{
                 list = roleService.getAll();   //使用ex_user里的数据
            }
            //  OkHttpUtils.
//            List<ShiroRole> list = new ArrayList<ShiroRole>();
//            for(int i=0;i<3;i++){
//            	ShiroRole s = new ShiroRole();
//            	s.setRole_id(i+"");
//            	s.setRole_name("角色"+i);
//            	list.add(s);
//            }
            if (list != null) {
                obj.put("Rows", list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("调用角色数据接口出错");
        }
        return obj;
    }

    @ResponseBody
    @RequestMapping(value = "/featureRole/getFeturePoleByCode")
    public Object getFeturePoleByCode(HttpServletRequest request) {
        String code = request.getParameter("code");
        ShiroRole s = new ShiroRole();
        List<Role> list = roleService.getAll();
        for (Role r : list) {
           if ((r.getRoleId() + "").equals(code)) {
                s.setRole_id(r.getRoleId() + "");
                s.setRole_name(r.getRoleName());
            }
        }
        return s;
    }

    @ResponseBody
    @RequestMapping(value = "/organization/getOrganizationByCode")
    public Object getOrganizationByCode(HttpServletRequest request) {
        String code = request.getParameter("code");
        OrgnizationNode u = new OrgnizationNode();
        u.setNodeId(Integer.parseInt(code));
        u.setNodeName("节点" + code);
        return u;
    }

    @ResponseBody
    @RequestMapping(value = "/user/all" ,produces = "application/json;charset=utf-8")
    public Object getUserAccount() {
        return  userService.getAll();
    }

    @ResponseBody
    @RequestMapping(value = "/user/getUserAccount",produces = "application/json;charset=utf-8")
    public Object getUserAccount(Integer account) {
        List<ShiroUser> list = userService.getAll();
        ShiroUser u = new ShiroUser();
        for (ShiroUser s : list) {
            if (s.getUserId().equals(account)) {
                u = s;
            }
        }
        return u;
    }

    /**
     * 提供json格式数据
     *
     * @param sortName  排序属性
     * @param sortOrder 排序类型 {asc, desc}
     * @param currPage  当前页
     * @param pageSize  页大小
     * @param session   request
     */
    @ResponseBody
    @RequestMapping("/user/list/json")
    public Object userlistJson(@RequestParam(value = "sortname", required = false, defaultValue = "") String sortName,
                               @RequestParam(value = "sortorder", required = false, defaultValue = "") String sortOrder,
                               @RequestParam(value = "page", required = false, defaultValue = "") String currPage,
                               @RequestParam(value = "pagesize", required = false, defaultValue = "") String pageSize,
                               HttpSession session) {

        Map<String, Object> obj = new HashMap<String, Object>();
        List<ShiroUser> list;
        try {
            //调用指定接口
            String dataInterfaces = (String) session.getAttribute("dataInterfaces");
            if(StringUtils.isNotBlank(dataInterfaces)){
                String roleUrl = dataInterfaces.split(",")[0];  //[0]是用户数据接口 ， [1]是角色数据接口
                String roleJson = OkHttpUtil.get(roleUrl, null);
                list = JsonUtil.jsonToList(roleJson, ShiroUser.class);
            }else{
                list = userService.getAll();   //使用ex_user里的数据
            }
            if (list != null) {
                obj.put("Rows", list);
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("调用用户数据接口出错");
        }
        return obj;
    }

    @ResponseBody
    @RequestMapping("/organization/list/json")
    public Object organizationlistJson(@RequestParam(value = "sortname", required = false, defaultValue = "") String sortName,
                                       @RequestParam(value = "sortorder", required = false, defaultValue = "") String sortOrder,
                                       @RequestParam(value = "page", required = false, defaultValue = "") String currPage,
                                       @RequestParam(value = "pagesize", required = false, defaultValue = "") String pageSize,
                                       HttpServletRequest request) {

        Map<String, Object> obj = new HashMap<String, Object>();
        try {
            List<OrgnizationNode> list = new ArrayList<OrgnizationNode>();
            for (int i = 1; i < 10; i++) {
                OrgnizationNode u = new OrgnizationNode();
                u.setNodeId(i);
                u.setNodeName("节点" + i);
                u.setParentNode(i / 3);
                list.add(u);
            }
            if (list != null) {
                obj.put("Rows", list);
            }
        } catch (Exception e) {
//            BapLogUtils.logError(JsonConstant.SEARCH_DATA_ERROR, e);
        }
        return obj;
    }


}
